package ajjj.kiviq.helpers;

import java.security.*;
import java.math.BigInteger;

/**
 * Helper methods for the User classes
 * 
 * @author Jonatan Strömsten
 */
public final class UserHelper {
    
    /**
     * Convert a String to an MD5 hash
     * 
     * @param s The String to convert
     * @return The resulting MD5 hash, lower cased
     */
    public static String toMD5(String s) {
	try {
	    MessageDigest md = MessageDigest.getInstance("MD5");
	    
	    md.update(s.getBytes());
	    BigInteger i = new BigInteger(1, md.digest());

	    return String.format("%032X", i).toLowerCase();
	    
	} catch (NoSuchAlgorithmException e) {	//Should never get thrown...
	    return null;
	}
    }
}
